package com.swlz;

import javax.servlet.http.HttpSession;

import com.swlz.exception.CodeException;
import com.swlz.vo.LoginUser;

import cn.hutool.core.util.StrUtil;

/**<pre>
 * - 常量
 * ClassName : com.swlz.Const
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020年8月1日 上午10:36:57
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. 
 * It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 **/
public class Const {
	
	private Const(){}
	/**
	 * 登录用户信息的名称
	 */
	public static final String LOGIN_USER_SESSION_NAME = "user";
	
	/**
	 * 获取在会话中存放的用户登录信息的属性名
	 * @param session - 会话
	 * @return - 用户登录信息
	 */
	public static final LoginUser getLoginUser(HttpSession session){
		return (LoginUser)session.getAttribute(LOGIN_USER_SESSION_NAME);
	}
	
	/**
	 * 获取在会话中存放的验证码的属性名
	 */
	public static final String CODE_NAME = "_CODE_NAME";
	
	/**
	 * 验证码的服务器端验证
	 * @param session
	 * @param code
	 * @throws CodeException 10:验证码不能为空 11:验证码已失效 12:验证码不匹配
	 */
	public static void codeVerify(HttpSession session,String code)throws CodeException{
		if(StrUtil.isEmpty(code)){
			throw new CodeException(10, "验证码不能为空");
		}
		if(session.getAttribute(CODE_NAME)==null){
			throw new CodeException(11, "验证码已失效");
		}
		
		if(!code.equals(session.getAttribute(CODE_NAME))){
			throw new CodeException(12, "验证码不匹配");
		}
	}

}
